package com.yituo.mjworld.mvc.form.support.user;

import com.yituo.mjworld.core.annotation.Rules;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 查询用户的表单
 */
public class QueryUser extends QueryForm {
  /**
   * 按ID查询
   */
  public static final int QUERY_BY_ID = 1;
  /**
   * 按昵称查询
   */
  public static final int QUERY_BY_NICKNAME = 2;


  @Rules(fieldName = "关键词", required = false)
  public String key = "";
  @Rules(fieldName = "查询的类型", min = "1", max = "2")
  public int queryType;
  @Rules(fieldName = "开始日期", required = true)
  public Date startDate;
  @Rules(fieldName = "结束日期", required = true)
  public Date endDate;

  /**
   * 初始化
   *
   * @return
   */
  @Override
  public boolean init(BaseForm target, Map<String, Object> params) {
    this.setBaseForm(target);
    parse(params);
    this.key = (String) this.getParam().get("key");
    try {
      this.queryType = Integer.parseInt((String) this.getParam().get("queryType"));
      String str_startDate = (String) this.getParam().get("startDate");
      String str_endDate = (String) this.getParam().get("endDate");
      if (!StringUtils.isEmpty(str_startDate) && !StringUtils.isEmpty(str_endDate)) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        this.startDate = format.parse(str_startDate);
        this.endDate = format.parse(str_endDate);
      }
    } catch (Exception e) {
      this.mErrorMsg = "查询类型应该为数字!";
      return false;
    }
    return true;
  }

  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public int getQueryType() {
    return queryType;
  }

  public void setQueryType(int queryType) {
    this.queryType = queryType;
  }

  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }

  public Date getEndDate() {
    return endDate;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }
}
